<template>
  <div class="wechatUser-form">
    <el-form :model="formData" :rules="rules" label-width="100px" ref="formData">
      <el-form-item label="昵称" prop="nickname">
        <el-input clearable v-model="formData.nickname"></el-input>
      </el-form-item>

      <el-form-item label="微信号" prop="wechatNumber">
        <el-input clearable v-model="formData.wechatNumber"></el-input>
      </el-form-item>

      <el-form-item label="用户分组" prop="groupId">
        <el-select clearable placeholder="请选择" v-model="formData.groupId">
          <el-option
            :key="item.id"
            :label="item.title"
            :value="item.id"
            v-for="item in userGroupList"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="submitForm('formData')" type="primary">保存</el-button>
        <el-button @click="resetForm('formData')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'WechatUserForm',
  props: {
    dataId: String,
    dialogFormVisible: Boolean
  },
  data() {
    return {
      userGroupList: [],
      formData: {
        nickname: null,
        wechatNumber: null,
        groupId: null
      },
      limit: 1,
      rules: {
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        wechatNumber: [
          { required: true, message: '请输入微信号', trigger: 'blur' }
        ],
        groupId: [
          { required: true, message: '请选择分组', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getUserGroupList()
    this.handleFormVisible()
  },
  watch: {
    dialogFormVisible(newVal, oldVal) {
      this.handleFormVisible()
    }
  },
  methods: {
    getUserGroupList() {
      var params = {
        sortPointer: {
          field: 'sort'
        }
      }
      this.$store.dispatch('userGroup/query', params).then(res => {
        this.userGroupList = res
      })
    },
    handleFormVisible() {
      this.adjuncts = []
      //异步更新DOM
      this.$nextTick(() => {
        //清除表单验证
        this.$refs['formData'].clearValidate()
      })
      if (this.dataId == null) {
        this.formData = {
          title: null,
          pictureCover: null,
          topic: null
        }
      } else {
        //异步获取获取详情数据
        this.getDataById(this.dataId)
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$store
            .dispatch('wechatUser/createOrUpdate', this.formData)
            .then(res => {
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
              this.$emit('handleDialog')
            })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    getDataById(dataId) {
      this.$store.dispatch('wechatUser/getByIdMoreDetail', dataId).then(res => {
        this.formData = res
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

